package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.LklMerchantSet;
import com.xmy.cultivate.entity.Merchant;
import com.xmy.cultivate.entity.Staff;
import com.xmy.cultivate.service.ILklMerchantSetService;
import com.xmy.cultivate.service.IStaffService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 拉卡拉商户设置 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-03-22
 */
@RestController
@RequestMapping("/api/lklMerchantSet")
public class LklMerchantSetController {

    @Autowired
    ILklMerchantSetService iLklMerchantSetService;

    @Autowired
    IStaffService iStaffService;

    @GetMapping("/getLklMerchantSetList")
    public Result getLklMerchantList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize){
        IPage<LklMerchantSet> page = new Page<>(pageNum, pageSize);
        QueryWrapper<LklMerchantSet> queryWrapper = new QueryWrapper<>();
        //根据时间排序
        queryWrapper.orderByDesc("created_at");
//        queryWrapper.eq("status",1);
        iLklMerchantSetService.page(page,queryWrapper);
        return new Result(Code.OK, new PageResult(page.getRecords(), page.getTotal(), page.getPages()));
    }

    /**
     * 选择商户
     * @param
     * @return
     */
    @GetMapping("/getLklMerchant")
    public Result getLklMerchant(){

        List<Merchant> merchantList = iLklMerchantSetService.getSchool();
        for (Merchant merchant:merchantList){
            List<LklMerchantSet> merchantSetList = iLklMerchantSetService.getMerchant(merchant.getSchoolId());
            if (merchantSetList.size() > 0){
                merchant.setLklMerchantSetList(merchantSetList);
            }

        }
        return new Result(Code.OK, merchantList, "查询成功");
    }

    //获取商户名称
    @GetMapping("/getLklMerchantName")
    public Result getLklMerchantName(){

        List<Merchant> merchantList = iLklMerchantSetService.getMerchantSchool();
        for (Merchant merchant:merchantList){
            QueryWrapper<Staff> staffQueryWrapper = new QueryWrapper<>();
            staffQueryWrapper.eq("status", 1);
//            staffQueryWrapper.eq("is_teacher", 1);
            staffQueryWrapper.eq("deleted", 0);
            staffQueryWrapper.eq("school_id", merchant.getSchoolId());
            List<Staff> staffList = iStaffService.list(staffQueryWrapper);

            merchant.setStaffList(staffList);

        }

        return new Result(Code.OK, merchantList, "查询成功");

    }


    @PostMapping("/saveLklMerchantSet")
    public Result saveLklMerchantSet(@RequestBody LklMerchantSet lklMerchantSet){
        QueryWrapper<LklMerchantSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id",lklMerchantSet.getTeacherId());
        queryWrapper.eq("status",lklMerchantSet.getStatus());

//        queryWrapper.eq("status",1);
        if (iLklMerchantSetService.count(queryWrapper) > 0){
            return new Result(Code.ERROR, "", "此账户名称已经存在");
        }
        QueryWrapper<LklMerchantSet> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("merchant_no",lklMerchantSet.getMerchantNo());
        queryWrapper1.eq("status",lklMerchantSet.getStatus());
        if (iLklMerchantSetService.count(queryWrapper1) > 0){
            return new Result(Code.ERROR, "", "此商户号已经存在");
        }

        boolean save = iLklMerchantSetService.save(lklMerchantSet);
        if (save){
            return new Result(Code.SAVE_OK, "", Code.SAVE_OK_MSG);
        }
        return new Result(Code.SAVE_ERROR, "", Code.SAVE_ERROR_MSG);
    }

    @PutMapping("/updateLklMerchantSet")
    public Result updateLklMerchantSet(@RequestBody LklMerchantSet lklMerchantSet){
        QueryWrapper<LklMerchantSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id",lklMerchantSet.getId());
        queryWrapper.eq("teacher_id",lklMerchantSet.getTeacherId());
        queryWrapper.eq("status",lklMerchantSet.getStatus());
        if (iLklMerchantSetService.count(queryWrapper) > 0){
            return new Result(Code.ERROR, "", "此账户已经存在");
        }
        QueryWrapper<LklMerchantSet> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.ne("id",lklMerchantSet.getId());
        queryWrapper1.eq("merchant_no",lklMerchantSet.getMerchantNo());
        queryWrapper1.eq("status",lklMerchantSet.getStatus());
        if (iLklMerchantSetService.count(queryWrapper1) > 0){
            return new Result(Code.ERROR, "", "此商户号已经存在");
        }
        boolean update = iLklMerchantSetService.updateById(lklMerchantSet);
        if (update){
            return new Result(Code.UPDATE_OK, "", Code.UPDATE_OK_MSG);
        }
        return new Result(Code.UPDATE_ERROR, "", Code.UPDATE_ERROR_MSG);
    }

    @DeleteMapping("/deleteLklMerchantSet")
    public Result deleteLklMerchantSet(@RequestBody LklMerchantSet lklMerchantSe){
        LklMerchantSet lklMerchantSet = iLklMerchantSetService.getById(lklMerchantSe.getId());
        if (lklMerchantSet == null){
            return new Result(Code.ERROR, "", "该商户设置不存在,无法删除");
        }
        boolean remove = iLklMerchantSetService.removeById(lklMerchantSe.getId());
        if (remove){
            return new Result(Code.DELETE_OK, "", Code.DELETE_OK_MSG);
        }
        return new Result(Code.DELETE_ERROR, "", Code.DELETE_ERROR_MSG);
    }
}
